package cn.hzxs.zjn.studentmanagement.controller;

import cn.hzxs.zjn.studentmanagement.domain.User;
import cn.hzxs.zjn.studentmanagement.sevice.UserService;
import cn.hzxs.zjn.studentmanagement.utils.JsonResult;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.lang.reflect.Array;
import java.util.*;


/**
 * @Author: Zjn ; Bring it on !!!
 * @Date: 2021/3/23 0023 15:33
 * Note:
 */
@RestController
public class UserController {
    @Autowired
    UserService userService;

    /*-----------------------登录功能-----------------------*/
    @PostMapping("/userLogin")
    public JsonResult userLogin(@RequestBody User user){
        HashMap<Object, Object> hashMap = new HashMap<>();
        if (userService.ifExistUsername(user.getUSERNAME()) == 0 && userService.ifExistPhone(user.getPHONE()) == 0) {
            hashMap.put("token",2);
            hashMap.put("userId","");
            return new JsonResult(hashMap,
                    "500","账号或手机号不存在");
        }
        if((user.getUSERNAME() != null?
                !userService.findPasswordByUsername(user.getUSERNAME()).equals(user.getPASSWORD()):
                !userService.findPasswordByPhone(user.getPHONE()).equals(user.getPASSWORD()))){
            hashMap.put("token",1);
            hashMap.put("userId","");
            return new JsonResult(hashMap,
                    "500","账号或密码错误");
        }
        if(user.getPHONE()!=null){
            hashMap.put("token",0);
            hashMap.put("userId",userService.findUserIdByPhone(user.getPHONE()).get("user_id"));
            hashMap.put("role",userService.findUserIdByPhone(user.getPHONE()).get("role"));
            return new JsonResult(hashMap,
                    "200","登录成功");
        }
        else {
            hashMap.put("token",0);
            hashMap.put("userId",userService.findUserIdByUserName(user.getUSERNAME()).get("user_id"));
            hashMap.put("role",userService.findUserIdByUserName(user.getUSERNAME()).get("role"));
            return new JsonResult(hashMap,
                    "200","登录成功");
        }
    }


    /*-----------------------注册功能-----------------------*/
    @PostMapping("/userReg")
    public JsonResult userReg(@RequestBody User user){
        HashMap<Object, Object> hashMap = new HashMap<>();
        if(userService.ifExistPhone(user.getPHONE()) == 0){
            userService.userReg(user);
            hashMap.put("token",0);
            hashMap.put("userId",userService.findUserIdByPhone(user.getPHONE()));
            return new JsonResult(hashMap,
                    "200","注册成功");
        }
        hashMap.put("token",1);
        return new JsonResult(hashMap,
                "500","账号或手机号已注册");
    }


    @GetMapping("/userGet")
    public JsonResult userGet(){
        List<Map> maps = userService.userGet();
        HashMap<Object, Object> hashMap = new HashMap<>();
        for (int i = 0; i < maps.size(); i++) {
            hashMap.put(i,maps.get(i));
        }
        return new JsonResult(hashMap);
    }


    @PostMapping("/personal")
    public JsonResult personal(@RequestBody User user){
        Map personal = userService.personal(user.getUSERID());
        return new JsonResult(personal);
    }


    @GetMapping("/teacher_personalGet")
    public JsonResult teacher_personalGet(){
        HashMap<Object, Object> hashMap = new HashMap<>();
        List<Map> maps = userService.teacher_planGet();
        for (int i = 0; i <maps.size() ; i++) {
            hashMap.put(i,maps.get(i));
        }
        return new JsonResult(hashMap);
    }


    @PostMapping("/uploadIcon")
    public String uploadIcon(@RequestParam("file") MultipartFile file){
        if (!file.isEmpty()) {
            try {
                /*
                 * 这段代码执行完毕之后，图片上传到了工程的跟路径； 大家自己扩散下思维，如果我们想把图片上传到
                 * d:/files大家是否能实现呢？ 等等;
                 * 这里只是简单一个例子,请自行参考，融入到实际中可能需要大家自己做一些思考，比如： 1、文件路径； 2、文件名；
                 * 3、文件格式; 4、文件大小的限制;
                 */
                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(new File(
                                file.getOriginalFilename())));
                System.out.println(file.getOriginalFilename());
                byte[] bytes = file.getBytes();
                System.out.println(Arrays.toString(bytes));
                out.write(bytes);
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            }

            return "上传成功";

        } else {
            return "上传失败，因为文件是空的.";
        }
    }
}
